package com.conan.app.dailyflickr.activity;

import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import android.util.Log;
import android.widget.RemoteViews;

import com.conan.app.dailyflickr.AppData;
import com.conan.app.dailyflickr.app.DailyPhotoData;
import com.conan.app.dailyflickr.provider.ProviderUtils.DailyInfo;
import com.conan.app.flickrdaily.R;

public class ConectivityReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if(intent == null) return;
        if(TextUtils.isEmpty(intent.getAction())) return;
        boolean connected = false;
        final ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        // check the handle before is processing
        if(AppData.CONNECT_CHANGE_PROCESS) return;
        AppData.CONNECT_CHANGE_PROCESS = true;
        
        NetworkInfo i = conMgr.getActiveNetworkInfo();
        if (i == null) connected = false;
        else if (!i.isConnected()) connected = false;
        else if (!i.isAvailable()) connected = false;
        else connected = true;
        if(!connected) return;
        
        DailyInfo info;
        AppWidgetManager manager = AppWidgetManager.getInstance(context);

        int[] appWidgetIds = manager.getAppWidgetIds(new ComponentName(context, DailyFlickrWidget.class));
        for(int appWidgetId : appWidgetIds){
            info = DailyFlickrWidget.getWidgetInfo(context, appWidgetId);
            startServiceUpdateAppWidget(context, appWidgetId, AppData.COMMAND_ACTION.refresh_new_day, info, AppData.NO_MAIN_LOADING);
        }
        
        AppData.CONNECT_CHANGE_PROCESS = false;;
    }
    
    private  void startServiceUpdateAppWidget(Context context, int appWidgetId,
            AppData.COMMAND_ACTION action, DailyInfo info, int id){
        AppWidgetManager manager = AppWidgetManager.getInstance(context);
        RemoteViews remoteView = DailyFlickrWidget.getRemoteView(context, info, null);
        if(id < 0){ 
            remoteView.setTextViewText(R.id.tv_flickr_status_refresh, 
                    context.getResources().getString(R.string.loading));
        }else{
            remoteView.setTextViewText(R.id.tv_main_message_text_id, 
                    context.getResources().getString(R.string.loading));
        }
        
        manager.updateAppWidget(appWidgetId, remoteView);
        Log.d(DailyPhotoData.TAG, "[startServiceUpdateAppWidget] updateAppWidget" +  info._page + " - " + id + " - " );
        
        Intent intent = new Intent(context, UpdateService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        intent.putExtra(AppData.COMMAND_ACTION.refresh_main_view.name(), id);
        intent.putExtra(AppData.ACTION_NAME, action.name());
        context.startService(intent);
    }
}